package com.takeoutsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.takeoutsystem.bean.Cabinet;
import com.takeoutsystem.bean.StorageRecordShow;
import com.takeoutsystem.bean.StorageRemain;
import com.takeoutsystem.bean.StorageRemainShow;
import com.takeoutsystem.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 顾客服务控制类
 * @author codeNoob2281
 */
@RestController
@Slf4j
public class CustomerController {

    @Autowired
    TakeoutHistoryShowService takeoutHistoryShowService;
    @Autowired
    RemainTakeoutShowService remainShowService;
    @Autowired
    TakeoutHistoryService historyService;
    @Autowired
    RemainTakeoutService remainService;
    @Autowired
    CabinetService cabinetService;

    /**
     * 查询取餐历史记录
     * @param userId 用户的openId
     * @return 历史记录列表
     */
    @GetMapping("/takeHistory")
    public List<StorageRecordShow> getStorageRecords(@RequestParam("userId") String userId){
        QueryWrapper<StorageRecordShow> wrapper = new QueryWrapper<>();
        wrapper.eq("take_user_id",userId);
        return takeoutHistoryShowService.list(wrapper);
    }


    /**
     * 模糊查询外卖柜单号
     * @param number 订单号（手机号后四位）
     * @return 查询结果,result=0表示未找到记录，result=1表示找到一条记录，result=2表示找到多条记录
     */
   @GetMapping("/getOrderFuzzy")
    public Map<String,Object> getOrderFuzzy(@RequestParam("number")String number){
        Map<String,Object> model=new HashMap<>();
        QueryWrapper<StorageRemainShow> wrapper=new QueryWrapper<>();
        wrapper.likeLeft("user_tel",number);
        List<StorageRemainShow> list=remainShowService.list(wrapper);
        if(list.size()==0){
            //找到0条
            model.put("result",0);
        }
        else if(list.size()==1){
            model.put("result",1);
            //找到一条记录，返回存柜信息
            model.put("order",list.get(0));
        }
        else{
            //找到多条，需要提示用户输入完整下单手机号查询存柜信息
            model.put("result",2);
        }
        return model;
   }
    /**
     * 精确查询外卖柜单号
     * @param number 订单号（下单手机号）
     * @return 查询结果,result=0表示未找到记录，result=1表示找到一条或多条记录并返回第一条记录
     */
    @GetMapping("/getOrderPrecise")
    public Map<String,Object> getOrderPrecise(@RequestParam("number")String number){
        Map<String,Object> model=new HashMap<>();
        QueryWrapper<StorageRemainShow> wrapper=new QueryWrapper<>();
        wrapper.eq("user_tel",number);
        //查找一条相关记录
        List<StorageRemainShow> list=remainShowService.list(wrapper);
        if(list.size()==0){
            //找到0条
            model.put("result",0);
        }
        else {
            //找到一条或多条记录，取第一条
            model.put("result",1);
            model.put("order",list.get(0));
        }
        return model;
    }

    /**
     * 完成取餐操作
     * 1.删除待取餐记录
     * 2.修改外卖柜状态为空
     * 3.添加取餐记录
     * @param cabId 柜号
     */
    @GetMapping("/completeOrder")
    @Transactional
    public void completeOrder(@RequestParam("cabId")String cabId,@RequestParam("userId") String userId){
        //获取待取餐记录信息
        StorageRemain info=remainService.getByCabNumber(cabId);

        //删除待取餐记录
        remainService.removeOrderInCab(cabId);
        //修改外卖柜状态为空
        QueryWrapper<Cabinet> wrapper=new QueryWrapper<>();
        wrapper.eq("serial_number",cabId);
        Cabinet cabinet=cabinetService.getOne(wrapper);

        cabinet.setStateCode(1);
        cabinetService.updateById(cabinet);
        //添加取餐记录
        historyService.addNewHistory(info,userId);
    }
}
